package com.example.treehole.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.treehole.R;
import com.example.treehole.entity.TabEntity;
import com.example.treehole.fragmnet.fragment1;
import com.example.treehole.fragmnet.fragment2;
import com.example.treehole.fragmnet.fragment3;
import com.example.treehole.utils.StatusBarUtil;
import com.example.treehole.utils.ViewFindUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private final ArrayList<Fragment> mFragments=new ArrayList<>();
//    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private final List<String> mTitles = new ArrayList<>();
    private final int[] mIconUnSelectIds = {
            R.mipmap.home, R.mipmap.analysis, R.mipmap.my};
    private final int[] mIconSelectIds = {
            R.mipmap.home_select, R.mipmap.analysis_select,R.mipmap.my_select};
    private final ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private View mDecorView;
    private CommonTabLayout tabLayout;


    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        mFragments.add(fragment1.newInstance());
        mFragments.add(fragment2.newInstance());
        mFragments.add(fragment3.newInstance());

        mTitles.add(getString(R.string.fragment_one));
        mTitles.add(getString(R.string.fragment_two));
        mTitles.add(getString(R.string.fragment_three));

        for (int i = 0; i < mTitles.size(); i++) {
            mTabEntities.add(new TabEntity(mTitles.get(i), mIconSelectIds[i], mIconUnSelectIds[i]));
        }

        mDecorView = getWindow().getDecorView();
        tabLayout = ViewFindUtils.find(mDecorView, R.id.tl_view_page);
        //tabLayout.setTabData(mTabEntities);
        tabLayout.setTabData(mTabEntities, this, R.id.fl_change, mFragments);

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                /*如果是最后一个页面就设置状态栏为沉浸式的。
                    这里有一个坑，设置状态栏为沉浸式的话，需要添加 fitsSystemWindows="true"
                    但activity里除了添加第一个fragment有效，后面就不起效果了
                    这里重写了frameLayout布局，再到每个xml文件进行声明才有效。
                 */
                if (position==2)
                    StatusBarUtil.setTranslucentForCoordinatorLayout(MainActivity.this,0);
                else
                    StatusBarUtil.setColorNoTranslucent(MainActivity.this, Color.WHITE);
            }
            @Override
            public void onTabReselect(int position) {
            }
        });
        //mTabLayoutViewPage.setCurrentTab(0);
        //显示未读红点
        //mTabLayoutViewPage.showDot(2);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getSupportFragmentManager().getFragments();
        if (getSupportFragmentManager().getFragments().size() > 0) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment mFragment : fragments) {
                mFragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}